package march.finalmarch22.march01;

import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class Reducer01 extends Reducer<Text, NullWritable,Text,NullWritable> {
    @Override
    protected void reduce(Text key, Iterable<NullWritable> values, Context context) throws IOException, InterruptedException {
        String[] split = key.toString().split("\\|");
        String name = split[0];
        String score = split[1];
        String avg = split[2];

        String[] split1 = avg.split(",");
        long total = 0;
        int size  = 0;
        for (String s : split1) {
            total += Long.parseLong(s);
            size++;
        }
        //转换成double类型提高精度
        double multiply = Double.parseDouble(String.valueOf(total))/Double.parseDouble(String.valueOf(size));

        String res = "姓名："+name+",课程："+score+",平均分："+multiply;

        context.write(new Text(res),NullWritable.get());
    }
}
